
$(function(){
	//添加数据
	$('#form_addButton').click(function(){
		formAddSubmit();
	});

	//Add dialog combobox auto load data
	for(var i=0;i<_columnCombobox4AddArray.length;i++){
		var _id = _columnCombobox4AddArray[i];
		var _data = _columnCombobox4AddJsonArray[i];
		//alert(_data);
		console.log(_data);
		var _jsonArray = $.parseJSON(_data);
		$("#"+_id).combobox("loadData", _jsonArray);
	}

	//Add dialog 中combobox 选择时，看看哪些字段不用输入，直接隐藏了
	for(var i=0;i<_columnCombobox4AddArray.length;i++){
		var _id = _columnCombobox4AddArray[i];
		$('#'+_id).combobox({
			onChange: function (n,o) {
				//取OptionId
				var data = $(this).combobox('getData');
				//alert(JSON.stringify(data));

				var keyName = $(this).combobox('getValue');
				var optionId = '';
				$.each(data,function(index,content){
					if(content.keyName==keyName){
						optionId = content.optionId;
					}
				})

				if(optionId==0){return;}

				//有隐藏的列，就先恢复一下数据
				for(var j=0;j<_columnDivArray.length;j++){
					$(_columnDivArray[j]).show();
				}

				//隐藏列 hiddenFileds(数据来源：data.js)
				for(var i=0; i<hiddenFileds.length; i++){
					var filedObj = hiddenFileds[i];
					var optionId1 = filedObj.optionId;
					if(optionId1 == optionId){
						var filedList = filedObj.filedList;
						for(var j=0; j<filedList.length; j++) {
							var json = filedList[j];
							var lineDiv = '#_' + json.text + '_line';
							$(lineDiv).hide();
						}
					}
				}
			}
		});
	}

	/*_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);
	_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);
	_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);
	_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);*/
	console.log(_parameter1_iframedialog);

	$('#form_add').form('load',_defaultValueJOStr);
	$('#form_add').form('load',_parameter1_iframedialog);

	var _owrRegionId = _parameter1_iframedialog.owrRegionId
	var userId = $("#id").val();
	//-2:default value, nothing to do for it
	//-1: waiting for loading data
	//0: not exist double random data
	//>=1: exist double random data, the number is regionChangeStatus >=1
	regionChangeStatus_Number = -2;
	//edit dialog
	if(userId != ''){
		$("#owrRegionId4Add").combobox({
			onChange: function (new_value, old_value) {
				if(_owrRegionId == new_value){return;}

				regionChangeStatus_Number = -1;

				var paramData = {};
				paramData["id"]=userId;
				//query ajax
				$.ajax({
					type:"POST",
					url:_path+_doublePairTaskExistAction,
					dataType:"json",
					data:paramData,
					success:function(msg){
						console.log(JSON.stringify(msg))
						if(JSON.stringify(msg).indexOf('error:')==0){
							window.parent.error(JSON.stringify(msg));
						}else{
							//成功
							if(msg[0].responseCode==1){
								regionChangeStatus_Number = msg[0].total;
							}
						}
					},
					error:function(er){
						window.parent.error(er);
					}
				});
			}
		});
	}

	$('#owrRegionId4Add').combobox('setValue', _owrRegionId);

});

/**
 * 添加：把id清空了。
 * 再问题申报中，生成任务时，加载所有信息同时，把申报ID加载成任务ID， 添加任务操作变成修改任务了，这个方法解决这个问题
 */
function formAddSubmitJustAdd(){
	$("#"+_primaryKey).val("");
	var _url = _path+_formAddAction;
	formAddSubmit(_url);
}

function formAddSubmit(){
	var _isAdd = true;
	var _url = _path+_formAddAction;
	var id = $("#"+_primaryKey).val();
	if(id!=''){
		_isAdd = false;
		_url=_path+_formEditAction;
	}
	formAddSubmitInner(_url, _isAdd);
}

/**
 * 增加form表单提交
 */
function formAddSubmitInner(_url, _isAdd){
	$('#form_add').form({
		url:_url,//参数中有id,就是修改操作，否则是添加操作
		onSubmit: function(){
			return $(this).form('validate');
		},
		success:function(data){
			//判断返回值不是 json 格式
			//if (!data.match("^\{(.+:.+,*){1,}\}$"))
			var checkJsonArr = /^\[(.+:.+,*)\]$/;
			var checkResult = checkJsonArr.test(data);
			if (!checkResult)
			{
				//普通字符串处理
				var er = data;
				if(window.parent.error && typeof(window.parent.error) == "function"){
					window.parent.error(er);
				}else{
					window.parent.parent.error(er);
				}
			}
			else
			{
				//通过这种方法可将字符串转换为对象
				var data1 = jQuery.parseJSON(data);
				//success
				if(data1[0].responseCode == 1){
					if(_isdialog4AddDialog==1){//dialog
						$("#form_add").form("clear");
						window.parent.datagrid_fresh();
						window.parent.closeWinCommon();
					}else{//tabs
						$.messager.alert("操作结果",data1[0].responseContent);
					}
				}else{
					$.messager.alert("操作结果",data1[0].responseContent);
				}
			}
		},
		error:function(er){
			if(window.parent.error && typeof(window.parent.error) == "function"){
				window.parent.error(er);
			}else{
				window.parent.parent.error(er);
			}
		}
	});

	if(_isAdd){
		$('#form_add').submit();
	}else {
		if(regionChangeStatus_Number == -2){
			$('#form_add').submit();
			return;
		}
		if(regionChangeStatus_Number == -1){
			alert('请重新提交');
			return;
		}
		if(regionChangeStatus_Number == 0){
			$('#form_add').submit();
			return;
		}
		if(regionChangeStatus_Number >= 1){
			$.messager.confirm('Confirm','TA有'+regionChangeStatus_Number+'条任务待处理，若切换区域将自动移除TA未完成任务，确认切换？',function(r) {
				if (r) {
					$('#form_add').submit();
					return;
				}
			})
		}
	}


}
